package com.block.api.controller.member;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.support.geo.Point;
import com.block.common.annotate.NeedToken;
import com.block.common.bean.CommonParam;
import com.block.common.bean.SessionBean;
import com.block.common.exception.ApiException;
import com.block.common.security.util.ApiUtil;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.AppEncrypt;
import com.market.member.model.param.UserSourceParam;
import com.market.member.service.IMemberAreacityPositionService;
import com.market.member.service.IMemberSourceDictInfoService;
import com.market.member.service.IMemberSourceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("api/member/source")
@Validated
public class MemberSourceController {

    @Autowired
    IMemberSourceDictInfoService memberSourceDictInfoService;
    @Autowired
    IMemberSourceInfoService memberSourceInfoService;
    @Resource
    IMemberAreacityPositionService memberAreacityPositionService;

    /**
     * 获取用户资质配置
     *
     */
    @NeedToken
    @RequestMapping(value = "/getCommonInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public String getCommonInfo(@RequestAttribute(value = "tranData") JSONObject tranData,
                                @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                                @RequestAttribute(value = "uri") String uri,
                                @RequestAttribute(value = "sessionBean") SessionBean sessionBean,
                                @RequestAttribute(value = "commonParam") CommonParam commonParam,
                                @RequestAttribute(value = "startDate") Date startdate,
                                @RequestAttribute(value = "ip") String ip) throws ApiException {

        String channelCode = commonParam.getChannelCode();
        ResponseVo responseVo = memberSourceDictInfoService.getCommonInfo(channelCode);
        return ApiUtil.dealReturnJson(responseVo, appEncrypt,  startdate, uri);
    }


    /**
     * 获取用户资质信息
     *
     */
    @NeedToken
    @RequestMapping(value = "/getSourceInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public String getSourceInfo(@RequestAttribute(value = "tranData") JSONObject tranData,
                                 @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                                 @RequestAttribute(value = "uri") String uri,
                                 @RequestAttribute(value = "sessionBean") SessionBean sessionBean,
                                 @RequestAttribute(value = "commonParam") CommonParam commonParam,
                                 @RequestAttribute(value = "startDate") Date startdate,
                                 @RequestAttribute(value = "ip") String ip) throws ApiException {

        ResponseVo responseVo = null;
        return ApiUtil.dealReturnJson(responseVo, appEncrypt,  startdate, uri);
    }

    /**
     * 上传用户资质信息, 并进行用户资质匹配
     *
     */
    @NeedToken
    @RequestMapping(value = "/updateSourceInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public String updateSourceInfo(@RequestAttribute(value = "tranData") JSONObject tranData,
                                 @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                                 @RequestAttribute(value = "uri") String uri,
                                 @RequestAttribute(value = "sessionBean") SessionBean sessionBean,
                                 @RequestAttribute(value = "commonParam") CommonParam commonParam,
                                 @RequestAttribute(value = "tranDataBean") UserSourceParam userSourceParam,
                                 @RequestAttribute(value = "startDate") Date startdate,
                                 @RequestAttribute(value = "ip") String ip) throws ApiException {

        userSourceParam.setUid(sessionBean.getUid());
        ResponseVo responseVo = memberSourceInfoService.updateUserSource(userSourceParam, commonParam, sessionBean.getUserType());
        return ApiUtil.dealReturnJson(responseVo, appEncrypt,  startdate, uri);
    }

    /**
     * 获取用户地理位置信息
     */
    @NeedToken
    @RequestMapping(value = "/getSourceInfoByPosition", method = {RequestMethod.POST, RequestMethod.GET})
    public String getSourceInfoByPosition(@RequestAttribute(value = "tranData") JSONObject tranData,
                                   @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                                   @RequestAttribute(value = "uri") String uri,
                                   @RequestAttribute(value = "sessionBean") SessionBean sessionBean,
                                   @RequestAttribute(value = "commonParam") CommonParam commonParam,
                                   @RequestAttribute(value = "startDate") Date startdate,
                                   @RequestAttribute(value = "ip") String ip) throws ApiException {


        //经度
        Double longitude = tranData.getDouble("longitude");
        //纬度
        Double latitude = tranData.getDouble("latitude");

        ResponseVo responseVo = memberAreacityPositionService.getSourceInfoByPosition(longitude,latitude);
        return ApiUtil.dealReturnJson(responseVo, appEncrypt,  startdate, uri);
    }


}
